import { Component, OnInit, ViewChild } from '@angular/core';
import {STColumn, STComponent, STData, STPage, STRes} from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import {SysRoleEditComponent} from "./edit/edit.component";

@Component({
  selector: 'app-sys-role',
  templateUrl: './role.component.html',
})
export class SysRoleComponent implements OnInit {
  url = `api/roles`;
  searchSchema: SFSchema = {
    properties: {
      keywords: {
        type: 'string',
        title: '',
        ui: {
          placeholder: '关键词',
        },
      },
    },
  };
  @ViewChild('st') private readonly st!: STComponent;
  columns: STColumn[] = [
    { title: 'ID', index: 'ID' },
    { title: '名称', index: 'name' },
    { title: '别名', index: 'alias' },
    { title: '状态', index: 'status' },
    { title: '描述', index: 'desc' },
    {
      title: '操作',
      buttons: [
        // { text: '查看', click: (item: any) => `/form/${item.id}` },
        { text: '编辑', type: 'static',
          modal: {
            component: SysRoleEditComponent,
            params: (record: STData) => record,
            modalOptions: { nzMaskClosable: false },
          },
          click: 'reload'
        },
        { text: '删除', type: 'del', click: (item: any) => this.del(item.id) },
      ]
    }
  ];
  res: STRes = {
    reName: {
      total: 'total',
      list: 'data.list',
    },
  };
  paginate: STPage = {
    showSize: true,
  };

  constructor(private http: _HttpClient, private modal: ModalHelper) { }

  ngOnInit(): void { }

  add(): void {
    this.modal
      .createStatic(SysRoleEditComponent, { i: { id: 0 } })
      .subscribe(() => this.st.reload());
  }

  /**
   * 删除
   * @param id 数据id
   */
  del(id: number): void {
    this.http.delete(`api/role/` + id).subscribe(() => {
      this.st.reload();
    });
  }

}
